﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebStudio.Business.Rule;
using WebStudio.Business.Entity;

public partial class Admin_ArticleAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin_User"] == null || Session["Admin_User"].ToString() == "")
        {
            Response.Redirect("~/Admin/Login.aspx");
        }

        if (!Page.IsPostBack)
        {
            txtDate.Text = DateTime.Now.ToShortDateString();
            ddlParentNavBind();
            GetArticleInfo();
        }
    }

    private ArticalEntity Info
    {
        get
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                return null;
            }
            else
            {
                try
                {
                    return ArticalRule.GetArticalEntity(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    private void GetArticleInfo()
    {
        ArticalEntity articalEntity = Info;
        if (articalEntity != null)
        {
            txtArticleName.Text = articalEntity.ArticalTitle;
            txtArticleOriginal.Text = articalEntity.ArticalOriginal;

            chkIsShow.Checked = Convert.ToBoolean(articalEntity.Status);
            chkIsBottom.Checked = Convert.ToBoolean(articalEntity.IsBottom);
            txtContent.Text = articalEntity.ArticalContent;
            //txtRank.Text = ArticleCategroyEntity.Rank.ToString();
            //chkIsBottom.Checked = Convert.ToBoolean(articalEntity.IsBottom);
            txtDate.Text = articalEntity.AddDate.ToString();
            txtTag.Text = articalEntity.ArticalTag.ToString();
            txtArticleOriginal.Text = articalEntity.ArticalOriginal.ToString();
            ddlParentNav.SelectedValue = articalEntity.CategoryInfo.CategoryId.ToString();
            //if (articalEntity.PId == 0)
            //{
            //    ddlParentNav.SelectedValue = ArticleCategroyEntity.CategoryId.ToString();
            //}
            //else
            //{
            //    ddlSubNavBind(Convert.ToInt32(ArticleCategroyEntity.PId));
            //    ddlParentNav.SelectedValue = ArticleCategroyEntity.PId.ToString();
            //    ddlSubNav.SelectedValue = ArticleCategroyEntity.CategoryId.ToString();
            //}
        }
    }

    private void ddlParentNavBind()
    {
        ddlParentNav.DataSource = CategoryRule.GetContentCategoryList();
        ddlParentNav.DataTextField = "CategoryName";
        ddlParentNav.DataValueField = "CategoryId";
        ddlParentNav.DataBind();
        ddlParentNav.Items.Insert(0, new ListItem("请选择...", "0"));
        //ddlSubNav.Items.Insert(0, new ListItem("请选择...", "0"));
    }
    //private void ddlSubNavBind(int id)
    //{
    //    ddlSubNav.DataSource = bCategory.GetSubList(id);
    //    ddlSubNav.DataTextField = "Name";
    //    ddlSubNav.DataValueField = "ID";
    //    ddlSubNav.DataBind();
    //}

    //protected void ddlParentNav_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    int id = Convert.ToInt32(ddlParentNav.SelectedValue);
    //    if (id != 0)
    //    {
    //        ddlSubNavBind(id);
    //    }
    //    else
    //    {
    //        ddlSubNav.Items.Clear();
    //    }
    //    ddlSubNav.Items.Insert(0, new ListItem("请选择...", "0"));
    //}

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string title = txtArticleName.Text.Trim();
        string original = txtArticleOriginal.Text.Trim();
        string tag = txtTag.Text.Trim();
        string content = txtContent.Text.Trim();
        //string category = ddlParentNav.SelectedValue;
        if (title == "")
        {
            return;
        }
        if (original == "")
        {
            return;
        }
        if (content == "")
        {
            return;
        }
        if (ddlParentNav.SelectedValue == "0")
        {
            return;
        }
        ArticalEntity articleEntity = new ArticalEntity();
        articleEntity.ArticalTitle = title;
        articleEntity.ArticalOriginal = original;
        articleEntity.ArticalTag = tag;
        articleEntity.CategoryInfo.CategoryId = Convert.ToInt32(ddlParentNav.SelectedValue);
        articleEntity.InnerCode = "1";
        articleEntity.ArticalImage = "";
        //if (ddlSubNav.Items.Count <= 1)
        //{
        //    articleEntity.CategoryId = Convert.ToInt32(category);
        //}
        //else
        //{
        //    articleEntity.CategoryId = Convert.ToInt32(ddlSubNav.SelectedValue);
        //}
        //if (string.IsNullOrEmpty(txtRank.Text.Trim()))
        //{
        //    articleEntity.Rank = 0;
        //}
        //else
        //{
        //    articleEntity.Rank = Convert.ToInt32(txtRank.Text.Trim());
        //}
        //articleEntity.IsBottom = chkIsBottom.Checked;
        articleEntity.UserInfo.UserId = 2;//暂时，记得修改
        if (chkIsShow.Checked)
        {
            articleEntity.Status = 1;
        }
        else
        {
            articleEntity.Status = 0;
        }
        if (chkIsBottom.Checked)
        {
            articleEntity.IsBottom = 1;
        }
        else
        {
            articleEntity.IsBottom = 0;
        }
        articleEntity.AddDate = Convert.ToDateTime(txtDate.Text.Trim());
        articleEntity.ArticalContent = txtContent.Text.Trim();
        articleEntity.ArticalType = 0;
        articleEntity.UpdateDate = DateTime.Now;
        if (Info == null)
        {
            //bArticle.AddArticle(articleEntity);
            articleEntity.ArticalHits = 0;
            if (ArticalRule.SaveArticalInfor(articleEntity))
            {
                //ArticalRule.SaveArticalInfor(articleEntity);
                ApplicationHelper.AddStartupScript(this, "if(confirm('添加成功，是否继续添加？')){location.href='ArticleAdd.aspx';}else{location.href='ArticleMge.aspx';}");
            }
            else
            {
                ApplicationHelper.AddStartupScript(this, "alert('添加失败！')");
            }
        }
        else
        {
            articleEntity.ArticalId = Convert.ToInt32(Request.QueryString["id"]);
            ArticalRule.SaveArticalInfor(articleEntity);
            ApplicationHelper.AddStartupScript(this, "alert('修改成功');location.href='ArticleMge.aspx';");
        }
    }
}
